#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : lee

import sys
import os
import logging
import re
from SceneCheckTool.checkScript.CheckBase import CheckBase

try:
    import maya.cmds as cmds
    import pymel.core as pm
    import jarryLibs.mayakit.MayaUtils as mu
    reload(mu)
except:
    pass

logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class CfxTimeConnectionsCheck(CheckBase):
    def __init__(self):
        super(CfxTimeConnectionsCheck, self).__init__()

    def check_node_time_connections(self):
        """
        描述：pgYeti节点检查的启动函数
        """
        error_nodes = []
        node_types = ["pgYetiMaya", "pgYetiGroom"]
        for node_type in node_types:
            for node in cmds.ls(type=node_type):
                if not cmds.listConnections(node + ".currentTime"):
                    error_nodes.append(node)
                    continue
                else:
                    conn_node = cmds.listConnections(node + ".currentTime")[0]
                    if cmds.nodeType(conn_node) != "time":
                        error_nodes.append(node)
        return error_nodes

    def check(self):
        result_proc = self.check_node_time_connections()
        if result_proc:
            result_log = u'Error:%s\n' % result_proc
        else:
            result_log = u'Succeed:没有问题\n'

        return result_log

    def fix(self):
        try:
            time_node = cmds.ls(type="time")[0]
            for node in self.check_node_time_connections():
                cmds.connectAttr(time_node + ".outTime", node + ".currentTime")
            return u'Succeed:没有问题\n'
        except:
            self.check()
